import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  OneToOne,
  PrimaryGeneratedColumn
} from 'typeorm'

import { SysDeptEntity, SysPostEntity, SysRoleEntity } from '.'
import { BaseEntity } from './base.entity'

@Entity()
export class SysUserEntity extends BaseEntity implements ISysUserEntity {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  userId: string

  @Column({ type: 'bigint' })
  deptId: string

  @Column()
  userName: string

  @Column()
  nickName: string

  @Column()
  email: string

  @Column()
  phone: string

  @Column()
  sex: string

  @Column()
  avatar: string

  @Column()
  password: string

  @Column()
  status: string

  @Column()
  delFlag: string

  @Column()
  loginIp: string

  @Column()
  loginDate: Date

  @Column()
  remark: string

  @OneToOne(() => SysDeptEntity)
  @JoinColumn({
    name: 'dept_id'
  })
  dept: SysDeptEntity

  @ManyToMany(type => SysRoleEntity, role => role.users)
  @JoinTable({
    name: 'sys_user_role',
    joinColumn: {
      name: 'user_id'
    },
    inverseJoinColumn: {
      name: 'role_id'
    }
  })
  roles: SysRoleEntity[]

  @ManyToMany(type => SysPostEntity, post => post.users)
  @JoinTable({
    name: 'sys_user_post',
    joinColumn: {
      name: 'user_id'
    },
    inverseJoinColumn: {
      name: 'post_id'
    }
  })
  posts: SysPostEntity[]
}
